//
//  SCEmoticonBar.swift
//  weibo
//
//  Created by 阿城 on 15/12/15.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit

enum EmoticonButtonType: Int{
    case Resent = 0
    case Normal = 1
    case Emoji = 2
    case LXH = 3
}

class SCEmoticonBar: UIStackView {

    
    var EmoticonBackclosure: ((type: EmoticonButtonType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var currentButton: SCEmoticonColletionButton?
    
    private func setupUI(){
        axis = .Horizontal
        distribution = .FillEqually
        
        addButton("compose_emotion_table_left", title: "最近", type: EmoticonButtonType.Resent)
        addButton("compose_emotion_table_mid", title: "默认", type: EmoticonButtonType.Normal)
        addButton("compose_emotion_table_mid", title: "Emoji", type: EmoticonButtonType.Emoji)
        addButton("compose_emotion_table_right", title: "LXH", type: EmoticonButtonType.LXH)
    }

    private func addButton(imgName: String, title: String, type:EmoticonButtonType){
        let btn = SCEmoticonColletionButton()
    
        btn.setBackgroundImage(UIImage(named: "\(imgName)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "\(imgName)_selected"), forState: UIControlState.Disabled)
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
//        btn.adjustsImageWhenHighlighted = false
//        btn.adjustsImageWhenDisabled = false

        btn.tag = type.rawValue
        addArrangedSubview(btn)
        btn.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchDown)
    }
    
    @objc private func buttonClick(button: SCEmoticonColletionButton){
        
        changeSelectedButton(button.tag)
//        currentButton?.enabled = true
//        currentButton = button
//        button.enabled = false
        
        //闭包回调
        let type = EmoticonButtonType(rawValue: button.tag)!
        EmoticonBackclosure?(type: type)
    }
    
    func changeSelectedButton(idx:Int){
        
        currentButton?.enabled = true
        currentButton = arrangedSubviews[idx] as? SCEmoticonColletionButton
        currentButton!.enabled = false
        
    }
    
}

class SCEmoticonColletionButton: UIButton {
    //关闭高亮
    override var highlighted:Bool{
        get{
            return false
        }
        set{
            
        }
    }
}
